package cn.edu.ujn.project.supermarket.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.project.common.annotation.Log;
import cn.edu.ujn.project.common.core.controller.BaseController;
import cn.edu.ujn.project.common.core.domain.AjaxResult;
import cn.edu.ujn.project.common.core.page.TableDataInfo;
import cn.edu.ujn.project.common.enums.BusinessType;
import cn.edu.ujn.project.common.utils.poi.ExcelUtil;
import cn.edu.ujn.project.supermarket.domain.GoodsInfor;
import cn.edu.ujn.project.supermarket.service.IGoodsInforService;
import tool.Generator;

/**
 * 商品信息Controller
 * 
 * @author zhaomh
 * @date 2023-11-14
 */
@Controller
@RequestMapping("/system/goods")
public class GoodsInforController extends BaseController
{
    private String prefix = "system/goods";

    @Autowired
    private IGoodsInforService goodsInforService;

    @RequiresPermissions("system:goods:view")
    @GetMapping()
    public String goods()
    {
        return prefix + "/goods";
    }

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("system:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsInfor goodsInfor)
    {
        startPage();
        List<GoodsInfor> list = goodsInforService.selectGoodsInforList(goodsInfor);
       
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("system:goods:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsInfor goodsInfor)
    {
        List<GoodsInfor> list = goodsInforService.selectGoodsInforList(goodsInfor);
        ExcelUtil<GoodsInfor> util = new ExcelUtil<GoodsInfor>(GoodsInfor.class);
        return util.exportExcel(list, "商品信息数据");
    }

    /**
     * 新增商品信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品信息
     */
    @RequiresPermissions("system:goods:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsInfor goodsInfor)
    {
    	goodsInfor.setBarCode(Generator.getString());
        return toAjax(goodsInforService.insertGoodsInfor(goodsInfor));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("system:goods:edit")
    @GetMapping("/edit/{barCode}")
    public String edit(@PathVariable("barCode") String barCode, ModelMap mmap)
    {
        GoodsInfor goodsInfor = goodsInforService.selectGoodsInforByBarCode(barCode);
        mmap.put("goodsInfor", goodsInfor);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品信息
     */
    @RequiresPermissions("system:goods:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsInfor goodsInfor)
    {
        return toAjax(goodsInforService.updateGoodsInfor(goodsInfor));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("system:goods:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(goodsInforService.deleteGoodsInforByBarCodes(ids));
    }
}
